﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace Nivlag
{

    static class CONSTANTS {

        public static string UrlWebDelimiter = "[delim]";

    } // CONSTANTS

    /// <summary>
    /// When the traverse method adds nodes for lists, it sets the "listid" attribute to 
    /// the url of the parent and the guid of the list (as a string).  Create an instance of this
    /// class to unpack those two items and reference them individually.
    /// </summary>
    public class WebListUrlParameters
    {

        private string myWebUrl;

        public string WebUrl
        {
            get { return myWebUrl; }
            set { myWebUrl = value; }
        }

        private string myListIdAsString;

        public string ListIdAsString
        {
            get { return myListIdAsString; }
            set { myListIdAsString = value; }
        }

        private Guid myListIdAsGuid;

        public Guid ListIdAsGuid
        {
            get { return myListIdAsGuid; }
            set { myListIdAsGuid = value; }
        }

        private string myWebRelativeUrl;

        public string MyWebRelativeUrl
        {
            get { return myWebRelativeUrl; }
            set { myWebRelativeUrl = value; }
        }

        public WebListUrlParameters(string unparsedParameters)
        {
            string left;
            string right;

            left = unparsedParameters.Substring(0, unparsedParameters.IndexOf(CONSTANTS.UrlWebDelimiter));
            right = unparsedParameters.Substring
                (unparsedParameters.IndexOf(CONSTANTS.UrlWebDelimiter) + CONSTANTS.UrlWebDelimiter.Length);

            this.myListIdAsString = right;
            this.myWebUrl = left;
            this.myListIdAsGuid = new Guid(right);

            if (myWebUrl.StartsWith("http"))
            {
                string relativeUrl = String.Empty;
                int protocolLength = 7; // assume http

                if (myWebUrl.StartsWith("https"))
                    protocolLength = 8;

                relativeUrl = myWebUrl.Substring(protocolLength); // strip out http://

                relativeUrl = relativeUrl.Substring(relativeUrl.IndexOf('/'));

                this.myWebRelativeUrl = relativeUrl;
            }
        } // TreeNodeParameters constructor
    }

} // namespace